package com.yunhe.abnormal.controller.data;

import com.yunhe.abnormal.domain.data.AbnormalSmsConfig;
import com.yunhe.common.exception.ArgumentErrorException;
import com.yunhe.common.exception.ObjectNotFoundException;
import com.yunhe.common.exception.UniqueConstraintsException;
import com.yunhe.common.exception.UserNotFoundException;
import com.yunhe.common.model.PageParam;
import com.yunhe.common.model.ResultObject;
import com.yunhe.abnormal.service.data.AbnormalSmsConfigService;
import com.yunhe.common.util.HeaderUtil;
import com.yunhe.common.util.QueryUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Optional;

@Api(value = "abnormalSmsConfig",description = "异常短信通知配置维护", tags = {"Data_AbnormalSmsConfig"})
@RestController
@RequestMapping("/abnormalSmsConfigs")
public class AbnormalSmsConfigController {
    @Autowired
    private HeaderUtil headerUtil;

    @Autowired
    private AbnormalSmsConfigService abnormalSmsConfigService;

    @ApiOperation("新增异常短信通知配置")
    @RequestMapping(method = RequestMethod.POST)
//    @OperationRecord(menu = "异常通知配置", actionType = SystemConsts.ACTION.ADD)
    public ResponseEntity<ResultObject> post(@RequestBody AbnormalSmsConfig abnormalSmsConfig)
            throws ArgumentErrorException, UniqueConstraintsException, ObjectNotFoundException, UserNotFoundException {
        abnormalSmsConfig.setId(null);
        AbnormalSmsConfig result = abnormalSmsConfigService.createConfig(abnormalSmsConfig);
        return new ResponseEntity<>(new ResultObject<>(result), HttpStatus.OK);
    }

    /**
     * 根据异接收人删除异常短信通知配置
     * @param id ID
     */
    @ApiOperation("根据接收人删除异常短信通知配置")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "ID", required = true, dataType = "long", paramType = "path")
    })
    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
//    @OperationRecord(menu = "异常通知配置", actionType = SystemConsts.ACTION.DELETE)
    public ResponseEntity<ResultObject> delete(@PathVariable("id") Long id) {
        abnormalSmsConfigService.delete(id);
        return new ResponseEntity<>(new ResultObject<>(null), HttpStatus.OK);
    }


    @ApiOperation("更新异常短信通知配置（更新全部字段）")
    @RequestMapping(method = RequestMethod.PUT)
//    @OperationRecord(menu = "异常通知配置", actionType = SystemConsts.ACTION.EDIT)
    public ResponseEntity<ResultObject> put(@RequestBody AbnormalSmsConfig abnormalSmsConfig)
            throws ArgumentErrorException, UniqueConstraintsException, ObjectNotFoundException, UserNotFoundException {
        AbnormalSmsConfig result = abnormalSmsConfigService.updateConfig(abnormalSmsConfig);
        return new ResponseEntity<>(new ResultObject<>(result), HttpStatus.OK);
    }

    @ApiOperation("更新异常短信通知配置（更新指定字段）")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "ID", required = true, dataType="long", paramType = "path"),
            @ApiImplicitParam(name = "userId", value = "接收人ID", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "phone", value = "手机号", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "dtime", value = "接收时间", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "remind", value = "是否通知", dataType = "boolean", paramType = "query")
    })
    @RequestMapping(value = "/{id}", method = RequestMethod.PATCH)
//    @OperationRecord(menu = "异常通知配置", actionType = SystemConsts.ACTION.EDIT)
    public ResponseEntity<ResultObject> patch(@PathVariable("id") Long id,
                                              @RequestParam("userId") Optional<Long> userId,
                                              @RequestParam("internationalCode") Optional<String> internationalCode,
                                              @RequestParam("phone") Optional<String> phone,
                                              @RequestParam("dtime") Optional<String> dtime,
                                              @RequestParam("remind") Optional<Boolean> remind)
            throws ArgumentErrorException, ObjectNotFoundException, UniqueConstraintsException, UserNotFoundException {
        AbnormalSmsConfig result = abnormalSmsConfigService.updateConfig(id, userId, internationalCode, phone, dtime, remind);
        return new ResponseEntity<>(new ResultObject<>(result), HttpStatus.OK);
    }

    /**
     * 根据ID获得异常短信通知配置
     * @param id ID
     */
    @ApiOperation("根据配置ID获得异常短信通知配置")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "ID", required = true, dataType = "string", paramType = "path")
    })
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    public ResponseEntity<ResultObject> get(@PathVariable("id") Long id)
            throws ObjectNotFoundException {
        AbnormalSmsConfig result = abnormalSmsConfigService.findById(id);
        return new ResponseEntity<>(new ResultObject<>(result), HttpStatus.OK);
    }

    /**
     * 根据接收人获得异常短信通知配置
     */
    @ApiOperation("根据接收人ID获得异常短信通知配置")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "userId",  dataType = "long", paramType = "query")
    })
    @RequestMapping(value = "/byUserId", method = RequestMethod.GET)
    public ResponseEntity<ResultObject> getByUserId(@RequestParam("userId") Long userId)
            throws ObjectNotFoundException {
        AbnormalSmsConfig result = abnormalSmsConfigService.findByUserId(userId);
        return new ResponseEntity<>(new ResultObject<>(result), HttpStatus.OK);
    }

    @ApiOperation("根据条件获得异常短信通知配置")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "phone", value = "电话", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "queryStr", value = "查询条件", dataType = "string", paramType = "query")
    })
    @RequestMapping(method = RequestMethod.GET)
    public ResponseEntity<ResultObject> get(@RequestParam("phone") Optional<String> phone,
                                            @RequestParam("queryStr") Optional<String> queryStr,
                                            @ModelAttribute PageParam pageParam) throws ObjectNotFoundException {
        Long firmId = headerUtil.getHeaderFirmId();
        Iterable<AbnormalSmsConfig> results = abnormalSmsConfigService.search(firmId, phone, queryStr, pageParam);
        if (results instanceof Page) {
            Page<AbnormalSmsConfig> pageContent = (Page<AbnormalSmsConfig>) results;
            List<AbnormalSmsConfig> content = pageContent.getContent();
            long totalElements = pageContent.getTotalElements();
            int totalPages = pageContent.getTotalPages();
            int page = QueryUtil.getPage(pageParam) + 1;
            int size = QueryUtil.getSize(pageParam);
            return new ResponseEntity<>(new ResultObject<>(content, page, size, totalElements, totalPages), HttpStatus.OK);
        } else {
            return new ResponseEntity<>(new ResultObject<>(results), HttpStatus.OK);
        }
    }
//
//    @ApiOperation("根据告警等级代码获得手机号")
//    @ApiImplicitParams({
//            @ApiImplicitParam(name = "firmId", value = "单位ID", dataType = "long", paramType = "query"),
//            @ApiImplicitParam(name = "alarmLevelNames", value = "告警等级", dataType = "String", paramType = "query")
//    })
//    @RequestMapping(value = "/phoneNumbers/byAlarmLevelName", method = RequestMethod.GET)
//    public ResponseEntity<ResultObject> getByAlarmLevelName(@RequestParam("firmId") Long firmId,
//                                                            @RequestParam("alarmLevelNames") String alarmLevelNames) {
//        List<String> results = abnormalSmsConfigService.getPhoneNumbersByAlarmLevel(firmId, alarmLevelNames);
//        return new ResponseEntity<>(new ResultObject<>(results), HttpStatus.OK);
//    }
}
